package org.scohen.juploadr.app;

import java.io.File;
import java.util.LinkedList;
import java.util.List;

import org.eclipse.swt.SWTException;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Display;
import org.scohen.juploadr.Messages;
import org.scohen.juploadr.event.RescaleListener;
import org.scohen.juploadr.ui.ReusableUIFactory;
import org.scohen.juploadr.ui.thumbnail.ThumbnailBuilder;
import org.scohen.juploadr.uploadapi.ImageUploadApi;
import org.scohen.juploadr.uploadapi.abilities.FileTypeNotSupportedException;

import com.bluecast.xml.Piccolo;

/**
 * @author steve
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class RescaleThread implements Runnable {
    private static final int NUM_WORKER_THREADS = 2;
    private Image scaled;
    private String imagePath;
    private RescaleListener listener;
    private static List<PhotoInfo> queue = new LinkedList<PhotoInfo>();
    private static ThreadGroup group;
    static {
        group = new ThreadGroup("worker-threads");
        for (int i = 0; i < NUM_WORKER_THREADS; i++) {
            RescaleThread t = new RescaleThread();
            Thread thread = new Thread(group, t);
            thread.setDaemon(true);
            thread.start();
        }
    }

    public static void addImage(String path, RescaleListener listener) {
        queue.add(new RescaleThread().new PhotoInfo(path, listener));
        synchronized (queue) {
            queue.notify();
        }
    }

    protected RescaleThread() {
    }

    public Image getScaledInstance() {
        return scaled;
    }

    public void run() {
        Display display = ReusableUIFactory.getInstance().getDisplay();
        while (true) {
            synchronized (queue) {
                if (queue.size() == 0) {
                    try {
                        queue.wait();
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                PhotoInfo pi = queue.remove(0);
                imagePath = pi.photoPath;
                listener = pi.listener;
            }
            File f = new File(imagePath);
            try {
                Image large = new Image(display,imagePath);
                ThumbnailBuilder builder = ImageUploadApi.getInstance().getAbilities().getUploadAbilities()
                        .getThumbnailBuilder(f);
                scaled = builder.buildLargeThumbnail(large,new Point(ImageAttributes.MAX_THUMBNAIL_DIMENSION,ImageAttributes.MAX_THUMBNAIL_DIMENSION));
                listener.rescaleDone(scaled);
            } catch (FileTypeNotSupportedException e) {
                listener.rescaleFailed(Messages.getString("juploadr.ui.error.format.unrecognized", f.getName()));
            }
        }
    }

    private class PhotoInfo {
        public String photoPath;
        public RescaleListener listener;

        public PhotoInfo(String path, RescaleListener listener) {
            this.listener = listener;
            this.photoPath = path;
        }
    }
}